<template>
  <el-dialog :close-on-click-modal='false' v-drag title="导出日志" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" @close='close'>
    <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules">
      <el-form-item label="密码" prop="password">
        <el-input v-emoji v-model="record.password" size="small" @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input v-emoji v-model="record.password2" size="small" @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { exportMgmtLog } from '@/api/system/oldLogData'
export default {
  name: 'SaveDialog',
  data () {
    var validatePassword = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error('不能为空'))
      } else {
        callback()
      }
    }
    var validatePassword2 = (rule, value, callback) => {
      if (value !== this.record.password) {
        callback(new Error('密码两次输入的不一致,请仔细确认!'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        password: '',
        password2: '',
        mode: '',
        level: '',
        startTime: '',
        endTime: '',
        srcIp: ''
      },
      recordCopy: {
        password: '',
        password2: '',
        mode: '',
        level: '',
        startTime: '',
        endTime: '',
        srcIp: ''
      },
      rules: {
        password: [{ required: false, validator: validatePassword, trigger: 'blur' }],
        password2: [{ required: false, validator: validatePassword2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.record = Object.assign(this.record, data)
    },
    handleFocus () {
      this.$refs.password = 'password'
    },
    handleBlur () {
      this.$refs.password = 'text'
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          exportMgmtLog(this.record).then(res => {
            if (res[0]) {
              let blob = new Blob([res[0]], { type: res[2] })
              let objectUrl = URL.createObjectURL(blob)
              let link = document.createElement('a')
              link.download = res[1] // 文件名字
              link.href = objectUrl
              // 触发下载
              link.click()
              URL.revokeObjectURL(objectUrl)
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.record = Object.assign({}, this.recordCopy)
      this.dialogVisible = false
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style>
.inputPsd {
  -webkit-text-security: disc;
}
</style>
